package top.mataiji.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.mataiji.manager.mapper.BookimgMapper;
import top.mataiji.manager.pojo.Bookimg;
import top.mataiji.manager.pojo.BookimgExample;
import top.mataiji.manager.service.BookImgService;

import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/20 - 03 - 20 - 10:41
 * @Description: top.mataiji.manager.service.impl
 * @version: 1.0
 */
@Service
public class BookImgServiceImpl implements BookImgService {

    @Autowired
    BookimgMapper bookimgMapper;

    @Override
    public List<Bookimg> findAllById(Integer bookId) {
        //创建查询对象
        BookimgExample bookimgExample = new BookimgExample();
        //设置查询参数
        bookimgExample.createCriteria().andBookidEqualTo(bookId);
        return bookimgMapper.selectByExample(bookimgExample);
    }

    /**
     * 添加书籍图片
     * @param bookImgs
     * @return
     */
    @Transactional
    @Override
    public int addBookImg(List<Bookimg> bookImgs) {
        int count = 0;
        for (int i = 0; i < bookImgs.size(); i++) {
//            count += bookimgMapper.insertSelective(bookImgs.get(i));
            count += bookimgMapper.insert(bookImgs.get(i));
        }
        return count;
    }

    @Override
    public int alertBook(List<Bookimg> bookImgs) {
        int count = 0;
        for (int i = 0; i < bookImgs.size(); i++) {
//            count += bookimgMapper.insertSelective(bookImgs.get(i));
            BookimgExample bookimgExample = new BookimgExample();
            bookimgExample.createCriteria().andIdEqualTo(bookImgs.get(i).getId());
            count += bookimgMapper.updateByExample(bookImgs.get(i), bookimgExample);
        }
        return count;
    }
}
